import java.awt.Color;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JLabel;
import javax.swing.border.LineBorder;


public class MyLabel extends JLabel {
	
	/** 2D Point representing the coordinate where mouse is, relative parent container */
    protected Point anchorPoint;
    private boolean isSelected = false;

	public MyLabel(String txt){
		super(txt);
		addListener();
		setFont(new Font("Serif", Font.PLAIN, 14));
		setLocation(0,0);
		setSize((int)(getPreferredSize().getWidth()+5),(int)(getPreferredSize().getHeight()+5));
        
	}
	
	private void addListener(){
	        
	        final MyLabel handle = this;
	        addMouseMotionListener(new MouseAdapter() {
	        	
	            @Override
	            public void mouseMoved(MouseEvent e) {
	               
	            	anchorPoint = e.getPoint();
					setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
	            }
	            
	            @Override
	            public void mouseDragged(MouseEvent e) {
	            	
	            	
	                int anchorX = anchorPoint.x;
	                int anchorY = anchorPoint.y;
	
	                Point parentOnScreen = getParent().getLocationOnScreen();
	                Point mouseOnScreen = e.getLocationOnScreen();
	                Point position = new Point(mouseOnScreen.x - parentOnScreen.x - anchorX, mouseOnScreen.y - parentOnScreen.y - anchorY);
	                
	                if(position.x >= 500){
	                	position.x = 500; 
	                } 
	                if(position.x <= 0){
	                	position.x = 0; 
	                }
	                if(position.y <= 0){
	                	position.y = 0; 
	                }
	                if(position.y >= 290){
	                	position.y = 290;
	                }
	                
	                setLocation(position);
	                repaint();
	                
	            }
	        });
	        
	        addMouseListener( new MouseListener(){

				@Override
				public void mouseClicked(MouseEvent arg0) {
					handle.setSelection();
				}

				@Override
				public void mouseEntered(MouseEvent arg0) {
					
					if(handle.isSelected()){
						//do nothing
					}
					else
					{
						setBorder(new LineBorder(Color.blue, 1));
					}
				}

				@Override
				public void mouseExited(MouseEvent arg0) {
					
					if(handle.isSelected()){
						//do nothing
					}
					else
					{
						setBorder(null);
					}
					
				}

				@Override
				public void mousePressed(MouseEvent arg0) {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void mouseReleased(MouseEvent arg0) {
					// TODO Auto-generated method stub
					
				}
	        	
	        });
	    }
	
	/**
     * Get the value of isSelected
     *
     * @return the value of isSelected
     */
    public boolean isSelected() {
        return isSelected;
    }

    /**
     * Set the value of isSelected
     *
     * @param autoSize new value of isSelected
     */
    public void setSelection() {
        if(isSelected){
        	setBorder(new LineBorder(Color.blue, 1));
        	isSelected = false;
        }
        else
        {
        	setBorder(new LineBorder(Color.red, 2));
        	isSelected = true;
        }
    }
    
    public void increaseSize(){
    	int current = getFont().getSize();
    	setFont(new Font("Serif", Font.PLAIN, current + 2));
    	setSize((int)(getPreferredSize().getWidth()+5),(int)(getPreferredSize().getHeight()+5));
    }
    
    public void decreaseSize(){
    	int current = getFont().getSize();
    	setFont(new Font("Serif", Font.PLAIN, current - 2));
    	setSize((int)(getPreferredSize().getWidth()+5),(int)(getPreferredSize().getHeight()+5));
    }
	
	
}
